package com.example.demo.java8;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shentong
 * @date 2018/8/6 10:28
 */
public class TravelContentUtils {

    private static final Pattern regex1 = Pattern.compile("ihref\\s*=\\s*\"http:\\s*");
    private static final Pattern regex2 = Pattern.compile("src=\"http:");
    private static final Pattern regex3 = Pattern.compile("src\\s*=\"\\s*");
    private static final Pattern regex4 = Pattern.compile("ihref\\s*=\"\\s*");
    private static final Pattern regex5 = Pattern.compile("src@=\"http:");
    private static final Pattern regex6 = Pattern.compile("<img");
    private static final Pattern regex7 = Pattern.compile("http://www.ctrip.com/Community/ItineraryWri/sendform.asp");
    private static final Pattern regex8 = Pattern.compile("</?(abbr|acronym|address|bdo|big|blockquote|button|body|pre)[^>]*>");
    private static final Pattern regex9 = Pattern.compile("(<style)+[^<>]*>[^\0]*(<\\/style>)+");
    private static final Pattern regex10 = Pattern.compile("(<script)+[^<>]*>[^\0]*(<\\/script>)+");
    private static final Pattern regex11 = Pattern.compile("\\s+=\\s+");
    private static final Pattern regex12 = Pattern.compile("\\r?\\n\", \"<br/>");
    private static final Pattern regex13 = Pattern.compile("(&nbsp;)+");
    private static final Pattern regex14 = Pattern.compile("(\\s| )+");
    private static final Pattern regex15 = Pattern.compile("(<iframe.*?>)(</iframe>){0,1}");
    private static final Pattern regex16 = Pattern.compile("(<(p|li|div|ol|ul)[^>]*>)+(\\s|&nbsp;)*(</(p|li|div|ol|ul)[^>]*>)+");
    private static final Pattern regex17 = Pattern.compile("<[^>]*>");
    private static final Pattern regex18 = Pattern.compile("http://(www\\.)?fishtrip\\.cn(/[a-zA-Z0-9\\&%_\\./\\-\\?=]*)?");
    private static final Pattern regex19 = Pattern.compile("[\"]{0}http://(www\\.)?[a-zA-Z0-9\\.]*?\\.qunar\\.com(/[a-zA-Z0-9\\&%_\\./\\-\\?=]*)?");
    private static final Pattern regex20 = Pattern.compile("[\"]{0}http://(www\\.)?[a-zA-Z0-9\\.]*?\\.elong\\.cn(/[a-zA-Z0-9\\&%_\\./\\-\\?=]*)?");
    private static final Pattern regex21 = Pattern.compile("</?(strong|span|ol|ul|li|hr|p|a|table|tr|th|td|iframe|img|br|b)[^>]*>");
    private static final Pattern regex22 = Pattern.compile("</?(?<g1>.*?)(\\s|>)");


    /**
     * 携程老游记
     *
     * @param travelContent
     * @return
     */
    public static String ctripTravelContent(String travelContent) {
        //过滤掉不保留的标记
        travelContent = htmlFilter(travelContent, false);

        //链接处理
        travelContent = regex1.matcher(travelContent).replaceAll("href=\"http:");
        travelContent = regex2.matcher(travelContent).replaceAll("src@=\"http:");

        //照片处理
        travelContent = regex3.matcher(travelContent).replaceAll("src=\"http://images3.ctrip.com/wri/images/");
        travelContent = regex4.matcher(travelContent).replaceAll("href=\"http://images3.ctrip.com/wri/images/");
        travelContent = regex5.matcher(travelContent).replaceAll("src=\"http:");
        travelContent = regex6.matcher(travelContent).replaceAll("<img onerror=\"this.style.display='none';this.parentNode.removeChild(this);\"");
        //老游记中包含大量这种无效链接
        travelContent = regex7.matcher(travelContent).replaceAll("#");

        return travelContent;
    }

    /**
     * 过滤掉不保留的标记
     *
     * @param travelContent 需要过滤的内容
     * @return
     */
    public static String htmlFilter(String travelContent, boolean isMobile) {
        travelContent = regex8.matcher(travelContent).replaceAll("");
        travelContent = regex9.matcher(travelContent).replaceAll("");
        travelContent = regex10.matcher(travelContent).replaceAll("");
        travelContent = regex11.matcher(travelContent).replaceAll("=");
        travelContent = regex12.matcher(travelContent).replaceAll("");
        travelContent = regex13.matcher(travelContent).replaceAll(" ");
        travelContent = regex14.matcher(travelContent).replaceAll(" ");
        travelContent = regex15.matcher(travelContent).replaceAll("$1</iframe>");
        //空标签
        travelContent = regex16.matcher(travelContent).replaceAll("");

        //保留标签
        travelContent = RegexUtils.replace(travelContent, regex21, matcher -> {
            String value = matcher.group();

            if (value.contains("/") && !StringUtils.contains(value, "http")) {
                return value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
            }
            Matcher matcher1 = regex22.matcher(value);
            matcher1.find();
            String tagName = matcher1.group("g1");
            return convertValue(tagName, value, isMobile);
        });

        travelContent = regex17.matcher(travelContent).replaceAll("");
        travelContent = travelContent.replace("@GSTravelLT", "<").replace("@GSTravelGT", ">");
        return travelContent;
    }


    public static String markFishTripLink(String travelContent) {
        String str = "<a target=\"_blank\" class=\"gs_a_poi\" href=\"$0\" >$0</a>";
        travelContent = regex18.matcher(travelContent).replaceAll(str);
        travelContent = regex19.matcher(travelContent).replaceAll(str);
        travelContent = regex20.matcher(travelContent).replaceAll(str);
        return travelContent;
    }

    public static String markSpecialLike(String travelContent) {
        travelContent = travelContent.replace("http://cn.mikecrm.com/qcTv2QR",
                "<a target=\"_blank\" class=\"gs_a_poi\" href=\"http://cn.mikecrm.com/qcTv2QR\" >http://cn.mikecrm.com/qcTv2QR</a>");
        return travelContent;
    }

    private static String convertValue(String tagName, String value, boolean isMobile) {
        //img 和 a 不做处理
        if ("a".equalsIgnoreCase(tagName)) {
            return value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
        } else if ("iframe".equalsIgnoreCase(tagName)) {
            String str = value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
            if (isMobile) {
                str = str.replace("height=\"500\"", "height=\"300\"");
            }
            str = str.replace("width=\"600\"", "style=\"width:100%\"");
            return str;
        } else if ("img".equalsIgnoreCase(tagName)) {
            return value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
        } else {
            return "@GSTravelLT" + tagName + "@GSTravelGT";
        }
    }


}
